package com.hilaver.dzmis.web.servlet.product;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiFactory;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.product.ProductLaunching;
import com.hilaver.dzmis.service.impl.ProductLaunchingServiceImpl;

/**
 * Servlet implementation class for Servlet: OrderServlet
 * 
 */
public class ProductLaunchingServlet extends
		com.hilaver.dzmis.web.servlet.AbstractBaseServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;
	
	public final static int ACTION_GET_VARIANT = 20;
	
	public final static int ACTION_ADD_TO_STOCK = 30;
	
	public final static int ACTION_PRODUCT_VALIDATE = 40;

	private ProductLaunchingServiceImpl plService;
	
	private ProductLaunching productLaunching;

	public ProductLaunchingServlet() {
		super();
		this.simpleName = ProductLaunching.class.getSimpleName();
		this.fullName = ProductLaunching.class.getName();
		this.plService = new ProductLaunchingServiceImpl();
		this.plService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			String outputStr = execute(request);
			if (this.action == ACTION_PRODUCT_VALIDATE) {
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);
		this.plService.setLocale((Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE));
		super.setAcUser(request, this.plService);
		String rtn = "";

		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";

			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			rtn = this.plService.edit(this.productLaunching);
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			rtn = this.plService.delete(this.id);
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			rtn = this.plService.get(this.id);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"

			break;
		case ACTION_GET_ALL:
			this.setId(request);
			rtn = this.plService.getAll(this.id);
			break;
		case ACTION_GET_VARIANT:
			this.setId(request);
			rtn = this.plService.getAllVariant(this.id);
			break;
		case ACTION_ADD_TO_STOCK:
			this.setId(request);
			String isStock = (String)this.getValue(request, "isStock");
			rtn = this.plService.addToStock(this.id, isStock);
			break;
		case ACTION_PRODUCT_VALIDATE:
			this.setId(request);
			this.fillObj(request);
			rtn = this.plService.validate(this.productLaunching);
			break;
		}

		return rtn;
	}

	protected void fillObj(HttpServletRequest request) throws Exception {
		super.fillObj(request);
		this.productLaunching = (ProductLaunching)this.obj;
		if(this.getValue(request, "productIdentificationId") != null){
			Integer productIdentificationId = Integer.parseInt((String)this.getValue(request, "productIdentificationId"));
			ProductIdentification pi = new ProductIdentification();
			pi.setId(productIdentificationId);
			this.productLaunching.setProductIdentification(pi);
		}
				
		Object biFactoryId = super.getValue(request, "biFactoryId");
		if(biFactoryId != null && !"".equals((String)biFactoryId)){
			BiFactory factory = new BiFactory();
			factory.setId(Integer.parseInt((String)biFactoryId));
			this.productLaunching.setBiFactory(factory);
		}
		
		
	}	
}